import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import javax.swing.JFrame;

public class WhiteBoardClient extends UnicastRemoteObject implements IWhiteBoardClient,
		Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4467166359490445706L;

	private IWhiteBoardServer server;

	public WhiteBoardModel getModel() {
		return model;
	}

	public InteractingBoard getBoard() {
		return board;
	}

	private WhiteBoardModel model;
	private InteractingBoard board;

	private Color color = Color.black;

	private String name = "default client";

	public Color getColor() {
		return color;
	}

	public void setColor(Color color) {
		this.color = color;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {

		this.name = name;

	}

	public WhiteBoardClient() throws RemoteException {
		super();
		try {

			Runtime.getRuntime().addShutdownHook(new Thread() {
				public void run() {
					try {
						server.logout(name);
					} catch (RemoteException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			});
			server = (IWhiteBoardServer) Naming.lookup("localhost" + "/Server");

			model = new WhiteBoardModel();
			board = new InteractingBoard(this);
		} catch (Exception e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
	}

	JFrame f = new JFrame();

	@Override
	public void receiveLine(Point start, Point end, Color color) throws RemoteException {
		model.addLine(start, end, color);
		// f.setSize(new Dimension(400, 400));
		// f.setContentPane(board);
		// f.setVisible(true);
		board.repaint();

	}

	public IWhiteBoardServer getServer() {
		return server;
	}

	public void connect() {
		try {
			Naming.rebind("localhost/" + name, this);
		} catch (RemoteException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (MalformedURLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			server.login(name, (IWhiteBoardClient) this, color);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
